<?php
// +----------------------------------------------------------------------
// | wttai [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.wttai.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 老司机 <wutongtai@wttai.com> <http://www.wttai.com>
// +----------------------------------------------------------------------

namespace Home\Model;
use Common\Model\BaseModel;

/**
 * 分类模型
 */
class TagsModel extends BaseModel{

    protected $_validate = array(
        array('name', 'require', '标识不能为空', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        array('name', '', '标识已经存在', self::VALUE_VALIDATE, 'unique', self::MODEL_BOTH),
        array('title', 'require', '名称不能为空', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
    );

    protected $_auto = array(
        array('model', 'arr2str', self::MODEL_BOTH, 'function'),
        array('model', null, self::MODEL_BOTH, 'ignore'),
        array('extend', 'json_encode', self::MODEL_BOTH, 'function'),
        array('extend', null, self::MODEL_BOTH, 'ignore'),
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', '1', self::MODEL_BOTH),
    );


    /**
     * 获取当前页面相关的标签
     * @param  int   $id 文章id
     * @param  int $number 相关标签
     * @return array     标签名数组
     * @author 老司机 <wutongtai@wttai.com>
     */
    public function getTags($id, $number = 10){
        /* 获取分类信息 */
        $map = array();
        if(is_numeric($id)){ //通过ID查询
            $map['article_id'] = $id;
            $tags_id= M('ArticleTags')->where($map)->field('tags_id')->select();
        }
        
        if(is_array($id)  && count($id)){
             $map['article_id'] = array('in',$id );
             $tags_id= M('ArticleTags')->where($map)->field('tags_id')->select();
        }
       if($tags_id){
           $tags_id= array_column($tags_id, 'tags_id');
           $where['id']=array('in',$tags_id);
           $where['effective']=1;
           return $this->where($where)->order('rank desc')->limit($number)->field('tag_name,id,name')->select();
       }
       return 0;
    }
    /**
     * 获取所有的标签
     * @param  int   $id 文章id
     * @param  int $number 相关标签
     * @return array     标签名数组
     * @author 老司机 <wutongtai@wttai.com>
     */
    public function getAllTags($id, $p=1, $limit=100){
        /* 获取分类信息 */
        $map = array();
        if(is_array($id)){
            $map['article_id'] = array('in',$id );
            $map['tag_id']=array('lt',1600);
        }
        $tags_id= M('ArticleTags')->where($map)->field('tags_id')->select();

        if($tags_id){
            $tags_id= array_column($tags_id, 'tags_id');
            $where['id']=array('in',$tags_id);
            $where['effective']=1;
            $where['tag_name']=array('neq','');
            return $this->where($where)->order('rank desc')->page($p, $limit)->field('tag_name,id,name')->select();
        }
        return 0;
    }
    /**
     * 获取所有的标签的个数
     * @param  int   $id 文章id
     * @param  int $number 相关标签
     * @return array     标签名数组
     * @author 老司机 <wutongtai@wttai.com>
     */
    public function getAllTagsCount($id){
         $map = array();
        if(is_array($id)){
            $map['article_id'] = array('in',$id );
            $map['tag_id']=array('lt',1600);
        }
        $tags_id= M('ArticleTags')->where($map)->field('tags_id')->select();
        if($tags_id){
            $tags_id= array_column($tags_id, 'tags_id');
            $where['id']=array('in',$tags_id);
            
            $where['effective']=1;
            $where['tag_name']=array('neq','');
            return $this->where($where)->select();
        }
        return 0;
    }

    /**
     * 获取分类树，指定分类则返回指定分类极其子分类，不指定则返回所有分类树
     * @param  integer $id    分类ID
     * @param  boolean $field 查询字段
     * @return array          分类树
     * @author 老司机 <wutongtai@wttai.com>
     */
    public function getTree($id = 0, $field = true){
        /* 获取当前分类信息 */
        if($id){
            $info = $this->info($id);
            $id   = $info['id'];
        }

        /* 获取所有分类 */
        $map  = array('status' => 1);
        $list = $this->field($field)->where($map)->order('sort')->select();
        $list = list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_', $root = $id);
        
        /* 获取返回数据 */
        if(isset($info)){ //指定分类则返回当前分类极其子分类
            $info['_'] = $list;
        } else { //否则返回所有分类
            $info = $list;
        }

        return $info;
    }

    /**
     * 获取指定分类的同级分类
     * @param  integer $id    分类ID
     * @param  boolean $field 查询字段
     * @return array
     * @author 老司机 <wutongtai@wttai.com>         
     */
    public function getSameLevel($id, $field = true){
        $info = $this->info($id, 'pid');
        $map = array('pid' => $info['pid'], 'status' => 1);
        return $this->field($field)->where($map)->order('sort')->select();
    }

    /**
     * 更新分类信息
     * @return boolean 更新状态
     * @author 老司机 <wutongtai@wttai.com>
     */
    public function update(){
        $data = $this->create();
        if(!$data){ //数据对象创建错误
            return false;
        }

        /* 添加或更新数据 */
        return empty($data['id']) ? $this->add() : $this->save();
    }

    /**
     * 获取指定分类子分类ID
     * @param  string $cate 分类ID
     * @return string       id列表
     * @author 老司机 <wutongtai@wttai.com>
     */
    public function getChildrenId($cate){
        $field = 'id,name,pid,title,link_id';
        $category = D('Category')->getTree($cate, $field);
        $ids[]    = $cate;
        foreach ($category['_'] as $key => $value) {
            $ids[] = $value['id'];
        }
        return implode(',', $ids);
    }

    /**
     * 查询后解析扩展信息
     * @param  array $data 分类数据
     * @author 老司机 <wutongtai@wttai.com>
     */
    protected function _after_find(&$data, $options){
        /* 分割模型 */
        if(!empty($data['model'])){
            $data['model'] = explode(',', $data['model']);
        }

        /* 分割文档类型 */
        if(!empty($data['type'])){
            $data['type'] = explode(',', $data['type']);
        }

        /* 分割模型 */
        if(!empty($data['reply_model'])){
            $data['reply_model'] = explode(',', $data['reply_model']);
        }

        /* 分割文档类型 */
        if(!empty($data['reply_type'])){
            $data['reply_type'] = explode(',', $data['reply_type']);
        }

        /* 还原扩展数据 */
        if(!empty($data['extend'])){
            $data['extend'] = json_decode($data['extend'], true);
        }
    }
    
    /**
     * 获取指定分类子分类标题
     * @param  string $cate 分类ID
     * @return string       title列表
     * @author 老司机 <wutongtai@wttai.com>
     */
    public function getChildrentitle($cate){
        $field = 'id,name,pid,title,link_id';
        $category = D('Category')->getTree($cate, $field);
        foreach ($category['_'] as $key => $value) {
            $result[$value['name']] = $value['title'];
        }
        return $result;
    }

    

}
